package com.bdqn.demo.service.impl;

import com.bdqn.demo.dao.UserMapper;
import com.bdqn.demo.pojo.Role;
import com.bdqn.demo.pojo.User;
import com.bdqn.demo.service.UserService;
import com.bdqn.demo.util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class UserServiceImpl implements UserService {
    //判断账号是否存在
    @Override
    public Integer registerIf(String username) {
        SqlSession sqlSession= MybatisUtil.getSqlSession();
        Integer result1=sqlSession.getMapper(UserMapper.class).registerIf(username);
        MybatisUtil.closeSqlSession(sqlSession);
        return result1;
    }
    //注册功能
    @Override
    public int addUser(User user) {
        SqlSession sqlSession=MybatisUtil.getSqlSession();
        int result=sqlSession.getMapper(UserMapper.class).addUser(user);
        sqlSession.commit();
        MybatisUtil.closeSqlSession(sqlSession);
        return result;
    }
    //修改功能
    @Override
    public int updateUser(User user) {
        SqlSession sqlSession=MybatisUtil.getSqlSession();
        int result=sqlSession.getMapper(UserMapper.class).updateUser(user);
        sqlSession.commit();
        MybatisUtil.closeSqlSession(sqlSession);
        return result;
    }

    //下拉列表
    @Override
    public List<Role> getRoleName() {
        SqlSession sqlSession=MybatisUtil.getSqlSession();
        List<Role> list=sqlSession.getMapper(UserMapper.class).getRoleName();
        MybatisUtil.closeSqlSession(sqlSession);
        return list;
    }

    @Override
    public int deleteUser(User user) {
        SqlSession sqlSession=MybatisUtil.getSqlSession();
        int result=sqlSession.getMapper(UserMapper.class).deleteUser(user);
        MybatisUtil.closeSqlSession(sqlSession);
        return result;
    }

    @Override
    public List<User> getUserList() {
        SqlSession sqlSession=MybatisUtil.getSqlSession();
        List<User> list=sqlSession.getMapper(UserMapper.class).getUserList();
        MybatisUtil.closeSqlSession(sqlSession);
        return list;
    }
}
